package com.qipa.netty.heartbeat;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * websocket单独存一个map
 */
@Slf4j
public class WebSocketChannelMap {


    private static Map<String, List<Channel>> channels = new HashMap<>();

    public static Map<String, List<Channel>> getChannels() {
        return channels;
    }

    public static void setChannels(Map<String, List<Channel>> channels) {
        WebSocketChannelMap.channels = channels;
    }

    public static void add(String gameId, Channel channel) {
        List<Channel> channelList = WebSocketChannelMap.channels.get(gameId);
        if (CollectionUtils.isEmpty(channelList)) {
            List<Channel> list = new ArrayList<>();
            list.add(channel);
            log.info("websocket添加连接之前，连接数：" + channels.size());
            channels.put(gameId, list);
            log.info("websocket添加连接之后，连接数：" + channels.size());
        } else {
            channelList.add(channel);
        }

    }
}
